package org.business.accounting.services;

import java.io.IOException;

import org.apache.tapestry5.*;
import org.apache.tapestry5.ioc.LoggerSource;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.ioc.annotations.Local;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.ioc.services.TypeCoercer;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestFilter;
import org.apache.tapestry5.services.RequestHandler;
import org.apache.tapestry5.services.Response;
import org.apache.tapestry5.services.Session;
import org.apache.tapestry5.services.ValueEncoderFactory;
import org.apache.tapestry5.services.ValueEncoderSource;
import org.business.accounting.interfaces.BasicOrder;
import org.business.accounting.service.ComputerDAO;
import org.business.accounting.service.ComputerDAOImpl;
import org.business.accounting.service.OrderDAO;
import org.business.accounting.service.OrderDAOImpl;
import org.business.accounting.service.ProductSourceDAO;
import org.business.accounting.service.ProductSourceDAOImpl;
import org.business.accounting.service.UserDAO;
import org.business.accounting.service.UserDAOImpl;
import org.slf4j.Logger;

/**
 * This module is automatically included as part of the Tapestry IoC Registry,
 * it's a good place to configure and extend Tapestry, or to place your own
 * service definitions.
 */
public class AppModule {
	
	public static void bind(ServiceBinder binder) {
		// binder.bind(MyServiceInterface.class, MyServiceImpl.class);

		// Make bind() calls on the binder object to define most IoC services.
		// Use service builder methods (example below) when the implementation
		// is provided inline, or requires more initialization than simply
		// invoking the constructor.
	}

	public static void contributeApplicationDefaults(
			MappedConfiguration<String, String> configuration) {
		// Contributions to ApplicationDefaults will override any contributions
		// to
		// FactoryDefaults (with the same key). Here we're restricting the
		// supported
		// locales to just "en" (English). As you add localised message catalogs
		// and other assets,
		// you can extend this list of locales (it's a comma separated series of
		// locale names;
		// the first locale name is the default when there's no reasonable
		// match).

		configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en,de");

		// The factory default is true but during the early stages of an
		// application
		// overriding to false is a good idea. In addition, this is often
		// overridden
		// on the command line as -Dtapestry.production-mode=false
		configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
	}
	
	public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration,
													@InjectService("orderDAO") final OrderDAO orderDAO) {
		
		// Example of injecting in appModule
		// URL: http://mail-archives.apache.org/mod_mbox/tapestry-users/200801.mbox/%3C40714f60801261255w5a0dbde1t948a68dcccb402f3@mail.gmail.com%3E
		
		ValueEncoderFactory factory = new ValueEncoderFactory() {

			public ValueEncoder create(Class arg0) {
				return new OrderEncoder(orderDAO);
			}
		};
		
		configuration.add(BasicOrder.class, factory);
	}
	
	public static ProductSourceDAO buildProductSourceDAO() {
		return new ProductSourceDAOImpl();
	}
	
	public static ComputerDAO buildComputerDAO() {
		return new ComputerDAOImpl();
	}
	
	public static UserDAO buildUserDAO() {
		return new UserDAOImpl();
	}
	
	public static OrderDAO buildOrderDAO() {
		return new OrderDAOImpl();
	}
}
